﻿
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using iBackupExplorer.Utils;

namespace iBackupExplorer.Data
{
    internal class CallDal : IDisposable
    {
        private static readonly CallDal _callDalInstance = new CallDal();
        private readonly CallsContext _callsContext;

        static CallDal()
        {
        }

        private CallDal()
        {
            _callsContext = new CallsContext();
        }

        public static CallDal GetInstance
        {
            get { return _callDalInstance; }
        }

        #region IDisposable Members

        public void Dispose()
        {
            _callsContext.Dispose();
            GC.SuppressFinalize(this);
        }

        #endregion

        public IList getAllCall()
        {
            List<Call> calls = (from items in _callsContext.Call
                                select items).ToList();

            foreach (Call items in calls)
            {
                AdressBook personn = AdressBookDal.GetInstance.getPersonn(items.address);
                if (personn != null)
                    items.FullName = (personn.First + " " + personn.Last).Trim();
                else
                    items.FullName = items.address;

                items.LongDate = Utils.Utils.ConvertFromUnix(items.date);
                TimeSpan time = TimeSpan.FromSeconds(items.duration);
                items.LongDuration = string.Format("{0:D2}m:{1:D2}s",time.Minutes, time.Seconds);
            }

            return calls;

        }
    }
}